{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from __future__ import print_function, absolute_import\n",
    "from gm.api import *\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 可以直接提取数据，掘金终端需要打开，接口取数是通过网络请求的方式，效率一般，行情数据可通过subscribe订阅方式\n",
    "# 设置token， 查看已有token ID,在用户-密钥管理里获取\n",
    "set_token('bf739ec28ad507f391a3c3e99ed0365007da88f8')\n",
    "\n",
    "# set_token('ca9c4532786a122aa5ceb3fd726432862e046f91')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           index       symbol  trade_date  market_value_total  \\\n",
      "299  SHSE.000300  SZSE.001289  2024-07-19           1472.9996   \n",
      "\n",
      "     market_value_circ  weight  \n",
      "299            23.4938     0.0  \n"
     ]
    }
   ],
   "source": [
    "data = stk_get_index_constituents(index='SHSE.000300')\n",
    "print(data.tail(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_volume - 按指定量委托\n",
    "order_volume(symbol, volume, side, order_type,position_effect, price=0,order_duration=OrderDuration_Unknown, order_qualifier=OrderQualifier_Unknown,account='')\n",
    "```\n",
    "OrderSide_Buy = 1             # 买入\n",
    "OrderSide_Sell = 2            # 卖出\n",
    "\n",
    "OrderType_Limit = 1\t       # 限价\n",
    "OrderType_Market = 2\t   # 市价(默认五档即成转限)\n",
    "\n",
    "ositionEffect_Open = 1                        # 开仓\n",
    "PositionEffect_Close = 2                       # 平仓\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_volume(symbol='SHSE.600000', \n",
    "             volume=10000, \n",
    "             side=OrderSide_Buy, # 订单委托方向\n",
    "             order_type=OrderType_Limit, # 限价\n",
    "             position_effect=PositionEffect_Open, # 开仓\n",
    "             price=11\n",
    "             )\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_value - 按指定价值委托\n",
    "order_value(symbol, value, side,order_type, position_effect, price=0, order_duration=OrderDuration_Unknown, order_qualifier=OrderQualifier_Unknown,account='')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以 11 元每股的价格买入价值为 100000 的 SHSE.600000,根据 volume = value / price,计算并取整得到 volume = 9000\n",
    "order_value(symbol='SHSE.600000', \n",
    "            value=100000, \n",
    "            price=11, \n",
    "            side=OrderSide_Buy, \n",
    "            order_type=OrderType_Limit, \n",
    "            position_effect=PositionEffect_Open\n",
    "            )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_percent - 按总资产指定比例委托\n",
    "order_percent(symbol, percent, side,order_type, position_effect, price=0, order_duration=OrderDuration_Unknown, order_qualifier=OrderQualifier_Unknown, account='')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当前总资产为 1000000。下限价单，以 11 元每股的价格买入 SHSE.600000,期望买入比例占总资产的 10%，根据 volume = nav * precent / price 计算取整得出 volume = 9000\n",
    "order_percent(symbol='SHSE.600000', \n",
    "              percent=0.1, \n",
    "              side=OrderSide_Buy, \n",
    "              order_type=OrderType_Limit, \n",
    "              position_effect=PositionEffect_Open,\n",
    "              price=11\n",
    "              )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_target_volume - 调仓到目标持仓量\n",
    "order_target_volume(symbol, volume, position_side, order_type, price=0, order_duration=OrderDuration_Unknown, order_qualifier=OrderQualifier_Unknown, account='')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当前 SHSE.600000 多方向持仓量为 0，期望持仓量为 10000，下单量为期望持仓量 - 当前持仓量 = 10000\n",
    "order_target_volume(symbol='SHSE.600000', \n",
    "                    volume=10000, \n",
    "                    position_side=PositionSide_Long, \n",
    "                    order_type=OrderType_Limit,\n",
    "                    price=13\n",
    "                    )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_target_value - 调仓到目标持仓额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当前 SHSE.600000 多方向当前持仓量为 0，目标持有价值为 100000 的该股票，根据 value / price 计算取整得出目标持仓量 volume 为 9000，目标持仓量 - 当前持仓量 = 下单量为 9000\n",
    "order_target_value(symbol='SHSE.600000', \n",
    "                   value=100000, \n",
    "                   position_side=PositionSide_Long, \n",
    "                   order_type=OrderType_Limit, \n",
    "                   price=11\n",
    "                   )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# order_target_percent - 调仓到目标持仓比例（总资产的比例）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# get_instrumentinfos - 查询交易标的基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get_instrumentinfos(symbols=None, exchanges=None, sec_types=None, names=None, fields=None, df=False)\n",
    "df_code = get_instrumentinfos(symbols=None, \n",
    "                                exchanges=None, \n",
    "                                sec_types=SEC_TYPE_STOCK, \n",
    "                                fields='symbol, listed_date, delisted_date', \n",
    "                                df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           symbol  sec_type exchange  sec_id sec_name sec_abbr  price_tick  \\\n",
      "0     SHSE.603766         1     SHSE  603766     隆鑫通用     LXTY        0.01   \n",
      "1     SHSE.600586         1     SHSE  600586     金晶科技     JJKJ        0.01   \n",
      "2     SHSE.603758         1     SHSE  603758     秦安股份     QAGF        0.01   \n",
      "3     SHSE.603065         1     SHSE  603065     宿迁联盛     SQLS        0.01   \n",
      "4     SHSE.688310         1     SHSE  688310     迈得医疗     MDYL        0.01   \n",
      "...           ...       ...      ...     ...      ...      ...         ...   \n",
      "5481  SZSE.000651         1     SZSE  000651     格力电器     GLDQ        0.01   \n",
      "5482  SZSE.300841         1     SZSE  300841     康华生物     KHSW        0.01   \n",
      "5483  SZSE.300417         1     SZSE  300417     南华仪器     NHYQ        0.01   \n",
      "5484  SZSE.002366         1     SZSE  002366     融发核电     RFHD        0.01   \n",
      "5485  SZSE.300623         1     SZSE  300623     捷捷微电     JJWD        0.01   \n",
      "\n",
      "                   listed_date             delisted_date  trade_n  ...  \\\n",
      "0    2012-08-10 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "1    2002-08-15 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "2    2017-05-17 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "3    2023-03-21 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "4    2019-12-03 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "...                        ...                       ...      ...  ...   \n",
      "5481 1996-11-18 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "5482 2020-06-16 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "5483 2015-01-23 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "5484 2010-03-12 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "5485 2017-03-14 00:00:00+08:00 2038-01-01 00:00:00+08:00        1  ...   \n",
      "\n",
      "      option_margin_ratio1  option_margin_ratio2  sec_type_ext option_type  \\\n",
      "0                      0.0                   0.0             0               \n",
      "1                      0.0                   0.0             0               \n",
      "2                      0.0                   0.0             0               \n",
      "3                      0.0                   0.0             0               \n",
      "4                      0.0                   0.0             0               \n",
      "...                    ...                   ...           ...         ...   \n",
      "5481                   0.0                   0.0             0               \n",
      "5482                   0.0                   0.0             0               \n",
      "5483                   0.0                   0.0             0               \n",
      "5484                   0.0                   0.0             0               \n",
      "5485                   0.0                   0.0             0               \n",
      "\n",
      "      call_or_put  underlying_symbol  is_strike_price_adjusted exercise_price  \\\n",
      "0                                                        False            0.0   \n",
      "1                                                        False            0.0   \n",
      "2                                                        False            0.0   \n",
      "3                                                        False            0.0   \n",
      "4                                                        False            0.0   \n",
      "...           ...                ...                       ...            ...   \n",
      "5481                                                     False            0.0   \n",
      "5482                                                     False            0.0   \n",
      "5483                                                     False            0.0   \n",
      "5484                                                     False            0.0   \n",
      "5485                                                     False            0.0   \n",
      "\n",
      "     conversion_price conversion_start_date  \n",
      "0                 0.0                  None  \n",
      "1                 0.0                  None  \n",
      "2                 0.0                  None  \n",
      "3                 0.0                  None  \n",
      "4                 0.0                  None  \n",
      "...               ...                   ...  \n",
      "5481              0.0                  None  \n",
      "5482              0.0                  None  \n",
      "5483              0.0                  None  \n",
      "5484              0.0                  None  \n",
      "5485              0.0                  None  \n",
      "\n",
      "[5486 rows x 24 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df_code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
